package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class delOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.println("请输入你要删除的书籍名称：");
        Scanner sc =new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = bookList.getUseSize();
        int index = -1;
        for (int i = 0; i <currentSize; i++) {
            Book temp= bookList.getBooks(i);
            if(temp.getName().equals(name)) {
                index = i;
                break;
            }
        }
        if(index < 0) {
            System.out.println("这里不存在这本书!");
        }else {
            for (int i = index; i <currentSize-1 ; i++) {
                Book book = bookList.getBooks(i+1);
                bookList.setBooks(book,i);
            }
            bookList.setBooks(null,currentSize-1);
            bookList.setUseSize(currentSize-1);
            System.out.println("删除成功！");
        }
    }
}
